package com.puntodonna.fogliocassa.server.task;

import com.google.appengine.api.taskqueue.DeferredTask;
import com.googlecode.objectify.ObjectifyService;
import com.puntodonna.fogliocassa.server.BCrypt;
import com.puntodonna.fogliocassa.shared.UserObj;

public class AddUser implements DeferredTask {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected String name;
	protected String password;
	
	public AddUser(String name, String password) {
		assert name != null;
		this.name = name;
		assert password != null;
		this.password = password;
		ObjectifyService.register(UserObj.class);
	}

	@Override
	public void run() {
		String hashed = BCrypt.hashpw(getPassword(), BCrypt.gensalt());
		UserObj userObj = new UserObj();
		userObj.setName(getName());
		userObj.setPassword(hashed);
		ObjectifyService.factory().begin().save().entity(userObj).now();
	}
	
	public String getPassword() {
		return password;
	}
	
	public String getName() {
		return name;
	}

}
